package Helpers;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

import DataAbstraction.Data;

public abstract class TableHelper {
	public static void connectLearnDataToTable(final TableViewer tableViewer, final Data data) throws Exception{
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setInput(data.getRawData());
		
		while ( tableViewer.getTable().getColumnCount() > 0 ) { //dispose old columns
			tableViewer.getTable().getColumns()[0].dispose();
		}
		
		for (int i = 0; i<data.getInputColumnsCount(); i++) //set input columns
		{
			final int ind = i;
			TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);		
			column.getColumn().setWidth(100);
			column.getColumn().setText(data.getHeaders()==null? "input" + i : data.getHeaders().get(data.getInputIndex(i)));			
			column.setLabelProvider(new ColumnLabelProvider() {
				  @Override
				  public String getText(Object element) {
					double[] values = (double[])element;
					String val; 
					try{
						val = Double.toString(values[data.getInputIndex(ind)]);						
					}
					catch (Exception ex){
						val = "not set";
					}
				    return val;
				  }				
				});
			column.setEditingSupport(new EditingSupport(tableViewer){

	            @Override
	            protected void setValue(Object element, Object value) {
	            	double[] el = (double[])element;
	            	try{	            		
		            	el[data.getInputIndex(ind)] = Double.parseDouble((String)value);		            	
	            	} catch (Exception ex){
	            		         		
	            	}	            	
	            	tableViewer.refresh();	
	            }

	            @Override
	            protected Object getValue(Object element) {
	                try {
						return Double.toString(((double[])element)[data.getInputIndex(ind)]);
					} catch (Exception e) {
						return Double.toString(Double.NaN);
					}
	            }

	            @Override
	            protected CellEditor getCellEditor(Object element) {
	                return new TextCellEditor(tableViewer.getTable());
	            }

	            @Override
	            protected boolean canEdit(Object element) {
	                return true;
	            }	            
	        });			
		}	
		
		for (int i = 0; i<data.getColumnsCount()-data.getInputColumnsCount(); i++) //set output columns
		{
			final int ind = i;
			TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);		
			column.getColumn().setWidth(100);
			column.getColumn().setText(data.getHeaders()==null? "output" + i : data.getHeaders().get(data.getOutputIndex(i)));				
			column.setLabelProvider(new ColumnLabelProvider() {
				  @Override
				  public String getText(Object element) {
					double[] values = (double[])element;
					String val; 
					try{
						val = Double.toString(values[data.getOutputIndex(ind)]);						
					}
					catch (Exception ex){
						val = "not set";
					}
				    return val;
				  }
				  @Override
				  public Color getBackground(Object element){
					  return tableViewer.getTable().getShell().getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);
				  }
				});
			column.setEditingSupport(new EditingSupport(tableViewer){

	            @Override
	            protected void setValue(Object element, Object value) {
	            	double[] el = (double[])element;
	            	try{	            		
		            	el[data.getOutputIndex(ind)] = Double.parseDouble((String)value);
	            	} catch (Exception ex){
	            		         		
	            	}	            	
	            	tableViewer.refresh();	
	            }

	            @Override
	            protected Object getValue(Object element) {
	                try {
						return Double.toString(((double[])element)[data.getOutputIndex(ind)]);
					} catch (Exception e) {
						return Double.toString(Double.NaN);
					}
	            }

	            @Override
	            protected CellEditor getCellEditor(Object element) {
	                return new TextCellEditor(tableViewer.getTable());
	            }

	            @Override
	            protected boolean canEdit(Object element) {
	                return true;
	            }	            
	        });			
		}
	
		tableViewer.refresh();	
	}
	
	public static void connectTestDataToTable(final TableViewer tableViewer, final Data data) throws Exception{
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setInput(data.getRawData());
		
		while ( tableViewer.getTable().getColumnCount() > 0 ) { //dispose old columns
			tableViewer.getTable().getColumns()[0].dispose();
		}
		
		for (int i = 0; i<data.getInputColumnsCount(); i++) //set input columns
		{
			final int ind = i;
			TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);		
			column.getColumn().setWidth(100);
			column.getColumn().setText(data.getHeaders()==null? "input" + i : data.getHeaders().get(data.getInputIndex(i)));			
			column.setLabelProvider(new ColumnLabelProvider() {
				  @Override
				  public String getText(Object element) {
					double[] values = (double[])element;
					String val; 
					try{
						val = Double.toString(values[data.getInputIndex(ind)]);						
					}
					catch (Exception ex){
						val = "not set";
					}
				    return val;
				  }				
				});
			column.setEditingSupport(new EditingSupport(tableViewer){

	            @Override
	            protected void setValue(Object element, Object value) {
	            	double[] el = (double[])element;
	            	try{	            		
	            		data.updateInputData();
		            	el[data.getInputIndex(ind)] = Double.parseDouble((String)value);
	            	} catch (Exception ex){
	            		         		
	            	}	            	
	            	tableViewer.refresh();	
	            }

	            @Override
	            protected Object getValue(Object element) {
	                try {
						return Double.toString(((double[])element)[data.getInputIndex(ind)]);
					} catch (Exception e) {
						return Double.toString(Double.NaN);
					}
	            }

	            @Override
	            protected CellEditor getCellEditor(Object element) {
	                return new TextCellEditor(tableViewer.getTable());
	            }

	            @Override
	            protected boolean canEdit(Object element) {
	                return true;
	            }	            
	        });			
		}	
		
		for (int i = 0; i<data.getColumnsCount()-data.getInputColumnsCount(); i++) //set output columns
		{
			final int ind = i;
			TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);		
			column.getColumn().setWidth(100);
			column.getColumn().setText(data.getHeaders()==null? "output" + i : data.getHeaders().get(data.getOutputIndex(i)));				
			column.setLabelProvider(new ColumnLabelProvider() {
				  @Override
				  public String getText(Object element) {
					double[] values = (double[])element;
					String val; 
					try{
						val = Double.toString(values[data.getOutputIndex(ind)]);						
					}
					catch (Exception ex){
						val = "not set";
					}
				    return val;
				  }
				  @Override
				  public Color getBackground(Object element){
					  return tableViewer.getTable().getShell().getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);
				  }
				});			
		}
	
		tableViewer.refresh();	
	}
}

